﻿using ASmile.Extend;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ASmile.Win
{
    public partial class FrmLoading : Form
    {
        public FrmLoading()
        {
            InitializeComponent();
        }

        public int ProgressMaxVal { set; get; }

        public bool IsShow { get; private set; }

        public void SetLoadingText(string text)
        {
            label1.Text = text;
        }

        public void ProgressAdd()
        {
            progressBar1.Value = progressBar1.Value + 1;
        }

        public void ProgressSet(int val)
        {
            progressBar1.Value = val;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            IsShow = true;
        }

        private void FrmLoading_Load(object sender, EventArgs e)
        {
            if (ProgressMaxVal > 0)
            {
                progressBar1.Style = ProgressBarStyle.Continuous;
                progressBar1.Maximum = ProgressMaxVal;
            }
        }
    }

    public static class LoadingHelper
    {
        static Control Owner { set; get; }

        static FrmLoading LoadingForm { set; get; }

        static int CurrProgress { set; get; }

        static Action OnWorkerAfter { set; get; }

        public static Task LoadingShow(this Control owner, Action WorkerAction, Action WorkerAfter = null, int maxProgress = 0)
        {
            return LoadingShow(owner, null, WorkerAction, WorkerAfter, maxProgress);
        }

        public static Task LoadingShow(this Control owner, string loadingtext, Action WorkerAction, Action WorkerAfter = null, int maxProgress = 0)
        {
            if (WorkerAction == null)
            {
                MessageBox.Show("执行操作为空");
                return null;
            }
            Owner = owner;
            OnWorkerAfter = WorkerAfter;
            CurrProgress = 0;

            Task.Factory.StartNew(Show);
            void Show()
            {
                Owner.InvokeTask(() =>
                {
                    LoadingForm = new FrmLoading();
                    LoadingForm.ProgressMaxVal = maxProgress;
                    if (loadingtext.IsNotEmpty())
                    {
                        LoadingForm.SetLoadingText(loadingtext);
                    }
                    LoadingForm.ShowDialog();
                });
            }

            var task =
            Task.Factory.StartNew(() =>
            {
                try
                {
                    WorkerAction.Invoke();
                }
                catch (Exception ex)
                {
                    LogHelper.LogObj.Error("Loading 过程发生异常", ex);
                    MessageBox.Show($"处理过程发生异常，{ex.Message}");
                }
                Close();
            });

            return task;
        }

        public static void LoadingProgressAdd(this Form owner)
        {
            Owner.InvokeTask(() =>
            {
                CurrProgress++;
                if (LoadingForm != null && !LoadingForm.IsDisposed)
                {
                    if (LoadingForm.IsShow)
                    {
                        LoadingForm.ProgressSet(CurrProgress);
                    }
                }
            });
        }

        public static void Close()
        {
            if (LoadingForm != null && !LoadingForm.IsDisposed)
            {
                LoadingForm.Close();
                LoadingForm.Dispose();
            }
            if (OnWorkerAfter != null)
            {
                Owner.InvokeTask(OnWorkerAfter);
            }
        }
    }
}